<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="dialogAddOrEdit.visible"
    :width="400"
    :title="dialogAddOrEdit.title"
    draggable
    close-icon="CloseBold"
    :close-delay="200"
    style="margin-top: 15vh"
    @close="closeDialog"
  >
    <el-form
      ref="addOrEditFormRef"
      v-loading="loading"
      :model="formData"
      :rules="rules"
      label-width="100"
      size=""
      style="padding-right: 10px; overflow: auto"
    >
      <el-row justify="start" style="margin-top: 10px">
        <el-col :span="20">
          <el-form-item label="所属企业" prop="lumpContractorCorpid">
            <el-input v-model="formData.corpName" readonly placeholder="请选择" />
            <el-button type="primary" plain style="position: absolute; right: -60px" @click="handleSelectCorp"
              >选择</el-button
            >
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="设备编码" prop="deviceCode">
            <el-input v-model="formData.deviceCode" placeholder="请输入设备编码" />
          </el-form-item>
        </el-col>

        <el-col :span="20">
          <el-form-item label="设备描述" prop="description">
            <el-input v-model="formData.description" placeholder="请输入设备描述" />
          </el-form-item>
        </el-col>

        <el-col :span="20">
          <el-form-item label="设备信息" prop="deviceInfo">
            <el-input
              v-model="formData.deviceInfo"
              type="textarea"
              style="width: 100%"
              :row="3"
              placeholder="请输入设备信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <select-corp
      v-if="dialogSelectCorp.visible"
      :dialog-select-corp="dialogSelectCorp"
      :corp-list-query="'common'"
      @corp-send-close="corpSendClose"
    ></select-corp>
    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">{{ dialogAddOrEdit.footBtn }}</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import SelectCorp from "@/views/manager/common/SelectCorp/index.vue";
import { AddCardReaderDevice, UpdateCardReaderDevice, QueryCardReaderDeviceDetail } from "@/api/device/index";
import { CardReaderDeviceEditForm } from "@/api/device/types";
import { FormRules } from "element-plus/es/components/form/src/types";
import type { UploadUserFile } from "element-plus";

//声明变量

const dialogSelectCorp = reactive<DialogOption>({
  visible: false,
  title: "选择所属企业",
  id: 0,
});

/* 关闭企业选择 */
function corpSendClose(corpInfo: any) {
  if (corpInfo) {
    formData.corpName = corpInfo.name;
    formData.corpCode = corpInfo.code;
  }
  dialogSelectCorp.visible = false;
}

/* 打开企业选择 */
function handleSelectCorp() {
  dialogSelectCorp.visible = true;
}

const props = defineProps({
  dialogAddOrEdit: {
    type: Object,
    default: () => ({
      visible: false,
      title: "新增",
      footBtn: "确认新增",
      id: 0,
      type: 1,
    }),
  },
});

const { dialogAddOrEdit } = toRefs(props);
const emit = defineEmits(["sendClose"]);
const loading = ref(false);

const formData = reactive<CardReaderDeviceEditForm>({});
const addOrEditFormRef = ref(ElForm);

//表单验证规则
const rules = reactive<FormRules<CardReaderDeviceEditForm>>({
  corpCode: [{ required: true, message: "请选择企业", trigger: "blur" }],
  deviceCode: [{ required: true, message: "请输入设备编码", trigger: "blur" }],
});

const initForm = async function () {
  if (dialogAddOrEdit.value.id) {
    loading.value = true;
    let res = await QueryCardReaderDeviceDetail(dialogAddOrEdit.value.id);
    loading.value = false;
    if (res.type === 0) {
      if (res.success) {
        Object.assign(formData, res.data);
        if (res.data.attachmentFiles) {
          let list = Array<any>();
          res.data.attachmentFiles.forEach((item: any) => {
            list.push({
              name: item.friendlyName,
              url: item.objectFilePath,
              status: "success",
              uid: item.id,
            });
          });
        }
      } else {
      }
    } else {
    }
  }
};

onMounted(() => {
  initForm();
});

/**
 * 关闭弹窗
 */
function closeDialog(needRefresh: boolean) {
  emit("sendClose", needRefresh);
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  addOrEditFormRef.value.resetFields();
  addOrEditFormRef.value.clearValidate();
  formData.id = undefined;
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  addOrEditFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      if (dialogAddOrEdit.value.id || dialogAddOrEdit.value.id > 0) {
        UpdateCardReaderDevice(formData)
          .then((res) => {
            if (res.type === 0) {
              if (res.success === true) {
                ElMessage.success(res.msg);
                closeDialog(true);
              } else {
                ElMessage.warning(res.msg);
              }
            } else {
              ElMessage.warning(res.msg);
            }
          })
          .catch((e) => {
            console.log(e);
          })
          .finally(() => (loading.value = false));
      } else {
        AddCardReaderDevice(formData)
          .then((resp) => {
            if (resp.type === 0) {
              if (resp.success === true) {
                ElMessage.success(resp.msg);
                closeDialog(true);
              } else {
                ElMessage.warning(resp.msg);
              }
            } else {
              ElMessage.warning(resp.msg);
            }
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}
</script>

<style lang="scss" scoped>
:deep(.el-form-item) {
  flex-wrap: wrap;
}

:deep(.el-input) {
  width: 100%;
}

:deep(.el-select) {
  width: 100%;
}
:deep(.el-cascader) {
  width: 100%;
}
:deep(.el-input__wrapper) {
  width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
:deep(.el-upload__tip) {
  display: inline-block;
  margin-left: 10px;
}
</style>
